package cn.itcast.hk;

import org.junit.Test;

import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class test {
    @Test
    public void test2() {
        int diliverNum = 0;//举例子的变量
        int queryMailNum = 0;//举例子的变量
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        if (queryMailNum == 0) {
            System.out.println("百分比为:" + 0 + "%");
        } else {
            String result = numberFormat.format((float) diliverNum / (float) queryMailNum * 100);
            System.out.println("百分比为:" + result + "%");
        }
    }
    @Test
    public void test1(){
        // JDK7 匿名内部类写法
        List<String> list = Arrays.asList("I", "love", "you", "too");
        Collections.sort(list, new Comparator<String>(){// 接口名
            @Override
            public int compare(String s1, String s2){// 方法名
                if(s1 == null)
                    return -1;
                if(s2 == null)
                    return 1;
                return s1.length()-s2.length();
            }
        });
        list.forEach(System.out::println);
    }
    @Test
    public  void test3(){
        // JDK8 Lambda 表达式写法
        List<String> list = Arrays.asList("I", "love", "you", "too");
        Collections.sort(list, (s1, s2) ->{// 省略参数表的类型
            if(s1 == null)
                return -1;
            if(s2 == null)
                return 1;
            return s1.length()-s2.length();
        });
        list.forEach(System.out::println);
    }
}
